﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Web;

using PushSharp;
using PushSharp.Android;
using PushSharp.Apple;
using PushSharp.Core;
using Newtonsoft.Json;

namespace Notification
{
    public enum DEVICE_TYPE { Apple, Android };
    public class Notification
    {
        private static PushBroker pushBroker;
        private DEVICE_TYPE deviceType;
        public Notification(DEVICE_TYPE deviceType)
        {
            try
            {
                pushBroker = new PushBroker();

                pushBroker.OnChannelCreated += pushBroker_OnChannelCreated;
                pushBroker.OnChannelDestroyed += pushBroker_OnChannelDestroyed;
                pushBroker.OnChannelException += pushBroker_OnChannelException;
                pushBroker.OnDeviceSubscriptionChanged += pushBroker_OnDeviceSubscriptionChanged;
                pushBroker.OnDeviceSubscriptionExpired += pushBroker_OnDeviceSubscriptionExpired;
                pushBroker.OnNotificationFailed += pushBroker_OnNotificationFailed;
                pushBroker.OnNotificationRequeue += pushBroker_OnNotificationRequeue;
                pushBroker.OnNotificationSent += pushBroker_OnNotificationSent;
                pushBroker.OnServiceException += pushBroker_OnServiceException;

                if (deviceType == DEVICE_TYPE.Apple)
                {
                    // config
                    string StrBytes = "MIIMpwIBAzCCDG4GCSqGSIb3DQEHAaCCDF8EggxbMIIMVzCCBucGCSqGSIb3DQEHBqCCBtgwggbUAgEAMIIGzQYJKoZIhvcNAQcBMBwGCiqGSIb3DQEMAQYwDgQIuMlYdl2mca8CAggAgIIGoBGnwp0DRwd7wbvHVG2JQXfj7ilKWtRxZnt3fVpV7cKhLeq//KCjkqlOlg54NHWzkRXa4OuwYuRPcfa+PPkv7+M7j1cMY1zslbYE+fIQmqmCqL9zuxvAujb32GofBfh1HH4wDLwN4aIg4Mc9yI671ob3KCRPM0/lGbceicjBtK8AjZ31273M0OokbHzOALBKcxQ1ScNXMUzFYnFHrNDnuK9VT7CY2rDknkBckLY6VMxzf1xTUwQufJ5KA3/tlAGD3GZBdqZdUYHWASW2Vv1nEfJEsM0SQHOi6oUAtrjkCIkS65/UXTzqKBlj3vwOSQEEySimD7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OoWnPbZK0liFFIfOrt0T/Ff6Wb5SYdfVimnzIhilKLhdXEcRvnKzXiMxdhRU4CHF1HNp8R5HGQc8elq50JsGBsPY88T/rPkhM5kL1vazvMd4vzN+z6+BsEyNqA6/lAxEdI9q6WYRtOm9Y2eSGQ2o+eMvGAjQQUx0WMr0tNWgVEExp9JOspLo1WX3L2pcdqPa60MRigKtqienrDz7o98l4F2/muDStZLlKa5xhIuqjITWgEqh6TKZ2K5ggAzgxZZ0TI7cITPi2hEkzIbxViuGFKsZjN/F5YgPja+U/u3AcoFnJdlTRJeg99fVnbFoQg2XC0ulH5APEWgiWjNx0z8nsYQfgjyW++v3IDTxG1tV7/ckNGqOZfAgtbITDAgFarEl8sDP2H/yaYAqq6g1LlFoULbu1HfQI+Ak7BpiEyevDVK8l5hK56wXZCMhxPDFMMCUGCSqGSIb3DQEJFDEYHhYARAB1AG8AbgBnAFYAYQBuAEwAYQBtMCMGCSqGSIb3DQEJFTEWBBRsKIhSAtSU9pEOja0qIWDJhujH4DAwMCEwCQYFKw4DAhoFAAQUs6ehAtLqpKCww05vD1IO5luHUWYECPMyZKSle3yvAgEB";
                    byte[] appleCert = this.GetBytes(StrBytes);
                    // config
                    pushBroker.RegisterAppleService(new ApplePushChannelSettings(true, appleCert, "rdsonca"));
                    this.deviceType = deviceType;
                }
                else
                    if (deviceType == DEVICE_TYPE.Android)
                    {
                        // config
                        pushBroker.RegisterGcmService(new GcmPushChannelSettings("883111302482", "AIzaSyASM3a1OVswxYTLajuxOcZEPLVdi7cXbNM", "com.example.meoiapplication"));
                        this.deviceType = deviceType;
                    }
                    else
                    {
                        pushBroker = null;
                        throw new Exception("Device Type is null");
                    }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private byte[] GetBytes(string str)
        {
            return Convert.FromBase64String(str);
        }

        public void sendNotification(string deviceID, string message)
        {
            try
            {
                if (pushBroker != null)
                {
                    if (deviceType == DEVICE_TYPE.Apple)
                    {
                        pushBroker.QueueNotification(new AppleNotification().ForDeviceToken(deviceID).WithAlert(message));
                        pushBroker.StopAllServices();
                    }
                    else
                        if (deviceType == DEVICE_TYPE.Android)
                        {
                            pushBroker.QueueNotification(new GcmNotification().ForDeviceRegistrationId(deviceID).WithJson("{\"alert\":\"" + message + "\"}"));
                            pushBroker.StopAllServices();
                        }
                        else
                        {
                            throw new Exception("Device Type is null");
                        }
                }
                else
                {
                    throw new Exception("PushBroker is null");
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        void pushBroker_OnServiceException(object sender, Exception error)
        {
        }

        void pushBroker_OnNotificationSent(object sender, INotification notification)
        {
        }

        void pushBroker_OnNotificationRequeue(object sender, NotificationRequeueEventArgs e)
        {
        }

        void pushBroker_OnNotificationFailed(object sender, INotification notification, Exception error)
        {
        }

        void pushBroker_OnDeviceSubscriptionExpired(object sender, string expiredSubscriptionId, DateTime expirationDateUtc, INotification notification)
        {
        }

        void pushBroker_OnDeviceSubscriptionChanged(object sender, string oldSubscriptionId, string newSubscriptionId, INotification notification)
        {
        }

        void pushBroker_OnChannelException(object sender, IPushChannel pushChannel, Exception error)
        {
        }

        void pushBroker_OnChannelDestroyed(object sender)
        {
        }

        void pushBroker_OnChannelCreated(object sender, IPushChannel pushChannel)
        {
        }
    }
}
